﻿using ChineseSchool.BLL;
using ChineseSchool.Models.Hethong;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ChineseSchool.Admin
{
    public partial class LessonDetails : System.Web.UI.Page
    {
        private B_Homework homeworkServices = new B_Homework();
        private B_Lessons lessonService = new B_Lessons();
        private B_Insert_Update_Delete insertupdate = new B_Insert_Update_Delete();
        B_User userService = new B_User();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["lessonid"]))
                {
                    if (Request.QueryString["lessonid"].Length > 0)
                    {
                        BindData(Convert.ToInt32(Request.QueryString["lessonid"]));
                    }
                }
            }
        }


        private void BindData(int lessonId)
        {
            var objLesson = lessonService.GetByPK(lessonId);

            if (objLesson != null)
            {
                lblLessonTitle.Text = objLesson.LessonTitle;
            }

            var lstFile = homeworkServices.GetFilesOfLesson(lessonId);
            rptAttach.DataSource = lstFile;
            rptAttach.DataBind();
        }

        private string GetContentType(string fileExtension)
        {
            if (string.IsNullOrEmpty(fileExtension))
                return string.Empty;

            string contentType = string.Empty;
            switch (fileExtension)
            {
                case ".htm":
                case ".html":
                    contentType = "text/HTML";
                    break;

                case ".txt":
                    contentType = "text/plain";
                    break;

                case ".doc":
                case ".rtf":
                case ".docx":
                    contentType = "Application/msword";
                    break;

                case ".xls":
                case ".xlsx":
                    contentType = "Application/x-msexcel";
                    break;

                case ".jpg":
                case ".jpeg":
                    contentType = "image/jpeg";
                    break;

                case ".gif":
                    contentType = "image/GIF";
                    break;

                case ".pdf":
                    contentType = "application/pdf";
                    break;
            }

            return contentType;
        }

        protected void RptAttachItemCommand(object source, RepeaterCommandEventArgs e)
        {
            var dowork = new M_Dowork();
            string userName = Session["Username"].ToString();
            string userPassword = Session["PassWord"].ToString();

            dowork.LessonId = Convert.ToInt32(Request.QueryString["lessonid"]);
            dowork.UserId =  userService.Selectby_username_password(userName, userPassword).UserID;

            insertupdate.insert("Dowork", "DoworkId", dowork);

            var fileName = (LinkButton)e.Item.FindControl("lbtFileAttach");
            var splitStr = fileName.Text.Split('.');
            var getExtension = splitStr[splitStr.Length - 1];

            Response.ContentType = GetContentType("." + getExtension);
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName.Text);
            Response.TransmitFile(Server.MapPath("/FilesUpload/" + fileName.Text));
            Response.End();
        }
    }
}